/**
 * 获取像素点的灰度值
 */
const wximg = function () {};
//4合1
function convert4to1(res) {
    let arr = [];
    for (let i = 0; i < res.length; i++) {
        if (i % 4 == 0) {
            let rule = 0.299 * res[i] + 0.587 * res[i + 1] + 0.114 * res[i + 2];
            if (rule > 200) {
                res[i] = 0;
            } else {
                res[i] = 1;
            }
            arr.push(res[i]);
        }
    }
    return arr;
}
//8合1
function convert8to1(arr) {
    let data = [];
    for (let k = 0; k < arr.length; k += 8) {
        let temp = arr[k] * 128 + arr[k + 1] * 64 + arr[k + 2] * 32 + arr[k + 3] * 16 + arr[k + 4] * 8 + arr[k + 5] * 4 + arr[k + 6] * 2 + arr[k + 7] * 1;
        data.push(temp);
    }
    return data;
}

/**
 * 根据微信小程序canvas组件生成符合打印要求的二维码数据
 */
wximg.prototype.overwriteImageData = function (data) {
    let sendWidth = data.width;
    let sendHeight = data.height;
    let arr = convert4to1(data.data);
    let sendImageData = convert8to1(arr);
    console.log('sendImageData==' + JSON.stringify(sendImageData));
    return {
        array: sendImageData,
        //Array.from(sendImageData),
        width: sendWidth / 8,
        height: sendHeight
    };
};
module.exports = wximg;
